<?php
require("config.php");
if (isset($_REQUEST['token'])) {
    $token = $_REQUEST['token'];
    if ($token==$lywq['Wechat']['token']) {
        
        $pwd = $lywq['Wechat']['passwordtext'];
        $strs = "123456789qwertyupasdfghjkzxcvbnm";
        $pwdNew = substr(str_shuffle($strs),mt_rand(0,strlen($strs)-7),6);
        
        $config = file_get_contents("config.php");
        $str = str_replace("'passwordtext' => '".$pwd."'","'passwordtext' => '".$pwdNew."'",$config);
        $type = $_REQUEST['type'];
        if ($lywq['Wechat']['RndPassword']==0) {
            exit(json_encode(['code' => 400, 'msg' => '未开启设置密码'], JSON_UNESCAPED_UNICODE));
        }
        elseif ($lywq['Wechat']['RndPassword']==1 && $type==1) {
            $hour = $lywq['Wechat']['RndPasswordH'];
            $timeN = date('Y-m-d H:i:s');
            $data = "<?php $"."pwdlog =array ( 'time' => '".$timeN."', 'pwdNew' => '".$pwdNew."');";
            if(!file_exists("pwdlog.php") || file_get_contents("pwdlog.php")==''){
                $res = file_put_contents("config.php",$str);
                if ($res>0) {
                    echo('首次执行成功，下次执行将在'.$hour.'小时后执行。');
                    file_put_contents("pwdlog.php",print_r($data, true));
                    exit();
                } else {
                    echo('密码设置失败！');
                }
                exit();
            } else {
                require('pwdlog.php');
                $timeO = $pwdlog['time'];
                $space = (strtotime($timeN)-strtotime($timeO))%86400/3600;
                if ($space>=$hour) {
                    $res = file_put_contents("config.php",$str);
                    if ($res>0) {
                        echo('密码设置成功，新密码：'.$pwdNew);
                        file_put_contents("pwdlog.php",print_r($data, true));
                    } else {
                        echo('密码设置失败！');
                    }
                } else {
                    echo('时间间隔小于'.$hour.'小时，不执行！');
                }
                exit();
            }
        }
        elseif ($lywq['Wechat']['RndPassword']==2 && $type==2) {
            $res = file_put_contents("config.php",$str);
            if ($res>0) {
                echo('密码设置成功，新密码：'.$pwdNew);
            } else {
                echo('密码设置失败！');
            }
        }
    } else {
        exit(json_encode(['code' => 202, 'msg' => '无效的token'], JSON_UNESCAPED_UNICODE));
    }
} else {
    exit(json_encode(['code' => 201, 'msg' => '缺少token参数'], JSON_UNESCAPED_UNICODE));
}
